import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:xinxuan/common/address_bar.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/common/goods_order_info_item.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/seckill_order_btns.dart';
import 'package:xinxuan/common/seckill_order_detail_goods_body.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/order_controller/seckill_order_detail_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/blindbox_order_status_bg.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:common_utils/common_utils.dart';

class SeckillOrderDetailPage extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SeckillOrderDetailPageController>(
      init: SeckillOrderDetailPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: controller.title.value,
            backgroundColor: pageBgColor,
          ),
          body: controller.seckillOrderDetailData.value.id! <= 0 ? Container() : ScrollConfigurationCommon(child: ListView(
            children: [
              AddressBar(
                consignee: '${controller.addressItem.value.consignee}', 
                mobile: '${controller.addressItem.value.mobile}', 
                provinceStr: '${controller.addressItem.value.provinceStr}', 
                cityStr: '${controller.addressItem.value.cityStr}', 
                districtStr: '${controller.addressItem.value.districtStr}', 
                address: '${controller.addressItem.value.address}', 
                id: 1,
                showRight: false,
              ),
              SizedBox(height: 12.h),
              SeckillOrderDetailGoodsBody(
                products: controller.seckillOrderDetailData.value.packages!, 
                statusStr: '${controller.seckillOrderDetailData.value.statusStr}'
              ),
              SizedBox(height: 12.h),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    GoodsOrderInfoItem(title: '订单编号', value: '${controller.seckillOrderDetailData.value.orderSn}', showCopy: true),
                    GoodsOrderInfoItem(title: '创建时间', value: '${controller.seckillOrderDetailData.value.createdAt}'),
                  ],
                ),
              ),
              SizedBox(height: 12.h),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    Column(
                      children: controller.seckillOrderDetailData.value.costs!.map((item) {
                        return GoodsOrderInfoItem(title: '${item.label}', value: '${item.value}');
                      }).toList(),
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
                      alignment: Alignment.centerRight,
                      child: Text.rich(
                        TextSpan(
                          text: controller.seckillOrderDetailData.value.status == 1 || controller.seckillOrderDetailData.value.status == 7 ?  '应付款:  ' : '实付款:  ',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.bold
                          ),
                          children: [
                            TextSpan(
                              text: '¥',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 12.sp,
                                fontWeight: FontWeight.bold
                              )
                            ),
                            TextSpan(
                              text: '${controller.seckillOrderDetailData.value.totalAmount}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold
                              )
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
              ),
              Container(
                // padding: EdgeInsets.all(12.w),
                margin: EdgeInsets.only(top: 12.h),
                child: _statusBar(controller),
              )
            ],
          )),
          bottomNavigationBar: BottomAppBar(
            elevation: 0,
            child: Container(
              height: controller.seckillOrderDetailData.value.status != 5 ? 56.h : 107.h,
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  controller.seckillOrderDetailData.value.status != 5 ? Container() : Container(
                    margin: EdgeInsets.only(bottom: 12.h),
                    child: ButtonUtil(
                      text: '在线客服', 
                      fontSize: 14.sp, 
                      borderRadius: 36.h, 
                      height: 36.h, 
                      textColor: mainTextColor,
                      showBorder: true,
                      bgColors: [
                        bgWhiteColor,
                        bgWhiteColor,
                      ],
                      onTop: () {
                        commonPageController.openWeChatCustomerServiceChat();
                      }
                    ),
                  ),
                  controller.seckillOrderDetailData.value.status == 1 || controller.seckillOrderDetailData.value.status == 5 || controller.seckillOrderDetailData.value.status == 8 ? SeckillOrderBtns(
                    btnWidth: 120.w,
                    status: controller.seckillOrderDetailData.value.status!, 
                    isClick: controller.seckillOrderDetailData.value.isCheck!,
                    toPayFun: () {
                      controller.toPay(controller.seckillOrderDetailData.value.payableAmount!, controller.seckillOrderDetailData.value.orderSn!, controller.seckillOrderDetailData.value.id!, controller.seckillOrderDetailData.value.status!);
                    }, 
                    cleanOrderFun: () {
                      controller.closeSeckillOrder();
                    },
                    confirmReceiptFun: () {
                      // controller.confirmReceipt();
                      Get.dialog(DelDialog(message: '确认收货吗？', onTap: () {
                        controller.confirmReceipt();
                      }));
                    },
                    customerServiceFun: () {
                      commonPageController.openWeChatCustomerServiceChat();
                    },
                    logisticsFun: () {
                      if (TextUtil.isEmpty(controller.seckillOrderDetailData.value.companyCode)) {
                        EasyLoading.showToast('快递单号异常');
                        return;
                      }
                      Get.toNamed(
                        '${AppRouters.COMMON_PAGE}${AppRouters.LOGISTICS_PAGE}',
                        arguments: {
                          'image': controller.seckillOrderDetailData.value.packages![0].image,
                          'title': controller.seckillOrderDetailData.value.packages![0].title,
                          'price': controller.seckillOrderDetailData.value.packages![0].price,
                          'trackingNumber': controller.seckillOrderDetailData.value.trackingNumber,
                          'companyCode': controller.seckillOrderDetailData.value.companyCode,
                          'companyTitle': controller.seckillOrderDetailData.value.companyTitle
                        }
                      );
                    },
                  ) : Container(
                    alignment: Alignment.center,
                    child: ButtonUtil(
                      text: '在线客服', 
                      fontSize: 14.sp, 
                      borderRadius: 36.h, 
                      height: 36.h, 
                      onTop: () {
                        commonPageController.openWeChatCustomerServiceChat();
                      }
                    ),
                  )
                ],
              )
            ),
          ),
        );
      },
    );
  }

  /// 根据状态展示内容
  Widget _statusBar(SeckillOrderDetailPageController controller) {
    if (controller.seckillOrderDetailData.value.status == 2 ||
        controller.seckillOrderDetailData.value.status == 3 ||
        controller.seckillOrderDetailData.value.status == 4) {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        child: Container(
          width: 351.w,
          height: 68.h,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(
                BlindboxOrderStatusBgUtil.statusMap['${controller.seckillOrderDetailData.value.status}']!
              ),
              fit: BoxFit.cover
            )
          ),
          child: controller.seckillOrderDetailData.value.status == 4 ? Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Text(
              '恭喜你获得${controller.seckillOrderDetailData.value.lottery == null ? '' : controller.seckillOrderDetailData.value.lottery!.product!.title}',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: mainTextColor,
                fontSize: 18.sp
              ),
            ),
          ) : Container(),
        ),
      );
    }

    if (controller.seckillOrderDetailData.value.status == 5 || controller.seckillOrderDetailData.value.status == 8) {
      return Container(
        height: 54.h,
        color: bgWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '中奖详情',
              style: TextStyle(
                color: textBlackColor,
                fontSize: 14.sp
              ),
            ),
            Expanded(child: Text(
              controller.seckillOrderDetailData.value.lottery == null ? '' : '${controller.seckillOrderDetailData.value.lottery!.product!.title}',
              textAlign: TextAlign.end,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            ))
          ],
        ),
      );
    }

    return Container();
  }
}